package 第二章数据结构.单链表.id826单链表;

import java.util.HashMap;
import java.util.Scanner;

public class Main {
    //记录当前插入的是第几个数
    static int cnt;
    //记录第几个数与节点
    static HashMap<Integer, MyNode> map = new HashMap<>();
    static MyNode head = new MyNode(-1,-1);
    public static void main(String[]args){
        Scanner scanner = new Scanner(System.in);
        int m = scanner.nextInt();
        scanner.nextLine();
        //处理过程
        while (m-- > 0){
            String s = scanner.nextLine();
            String[] cmd = s.split(" ");
            if(cmd[0].equals("H")){
                int x = Integer.parseInt(cmd[1]);
                head_insert(x);
            }else if(cmd[0].equals("D")){
                int k = Integer.parseInt(cmd[1]);
                delete(k);
            }
            else {
                int k = Integer.parseInt(cmd[1]);
                int x = Integer.parseInt(cmd[2]);
                insert(k,x);
            }
        }
        //处理输出
        MyNode cur = head.next;
        while (cur != null){
            System.out.print(cur.val+ " ");
            cur = cur.next;
        }
    }
    static class MyNode{
        int val;
        MyNode next;
        int cnt;
        MyNode(int val,int cnt){
            this.val = val;
            this.cnt = cnt;
        }
    }
    //头插入
    static void head_insert(int x){
        MyNode next = head.next;
        cnt++;
        MyNode cur = new MyNode(x,cnt);
        head.next = cur;
        cur.next = next;
        map.put(cnt,cur);
    }
    //删除第k个插入的数后面的数
    static void delete(int k){
        if(k == 0){
            head.next = head.next.next;
            return;
        }
        MyNode cur = map.get(k);
        cur.next = cur.next.next;
    }
    //在第k个插入的数后面插入一个数
    static void insert(int k , int x){
        cnt++;
        MyNode cur = map.get(k);
        MyNode next = cur.next;
        MyNode temp = new MyNode(x,cnt);
        cur.next = temp;
        temp.next = next;
        map.put(cnt,temp);
    }
}


